
from amscms.core.utils.email import send_email, send_emails
from django.conf import settings
from django.contrib.auth.models import User
from django.core import mail
from django.test import TestCase
from django.test.client import Client

class ViewsTest(TestCase):
    
    def setUp(self):
        self.user1 = User.objects.create_user('user1', 'user@test.com', 'user1pass')
        self.c  = Client()
        
    def testUpdateGlobalStats(self):
        response = self.c.get('/core/stats/')
        self.assertEquals(response.status_code, 200)
        self.assertEquals(response.template[0].name, 'stats_template.html')
        context = response.context[0]
        self.assertTrue(context.has_key('total_registered_ads'))
        
class EmailTest(TestCase):
    '''
    Take a look at here please:
    http://docs.djangoproject.com/en/1.2/topics/testing/#e-mail-services
    '''
    
    def setUp(self):
        self.c = Client()
        
    def testSendEmail(self):
        context = {}
        context['subject'] = 'Test'
        context['body'] = 'Sample Body'
        send_email('behrooz@amscms', context, settings.DEFAULT_FROM_EMAIL)
        
        self.assertEquals(len(mail.outbox), 1)
        self.assertEquals(mail.outbox[0].subject, 'Test')
        self.assertEquals(mail.outbox[0].from_email, settings.DEFAULT_FROM_EMAIL)
        self.assertEquals(mail.outbox[0].to[0], 'behrooz@amscms')
        
    def testSendEmails(self):
        context = {}
        context['subject'] = 'Test All'
        context['body'] = 'Sample Body for all'
        send_emails(['behrooz@amscms', 'sid@amscms'], context)
        
        self.assertEquals(len(mail.outbox), 2)
        self.assertEquals(mail.outbox[0].subject, 'Test All')
        self.assertEquals(mail.outbox[1].subject, 'Test All')
        self.assertEquals(mail.outbox[0].to[0], 'behrooz@amscms')
        self.assertEquals(mail.outbox[1].to[0], 'sid@amscms')
        
